﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CNFM_CROCODILE_LOGO.Core.Domain.ViewModels;
using CNFM_CROCODILE_LOGO.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CNFM_CROCODILE_LOGO_Web.Controllers
{
    /// <summary>
    /// 产品管理
    /// </summary>
    public class SpeciesManageController : Controller
    {
        private IBasicServices _pro;
        public SpeciesManageController(IBasicServices pro)
        {
            this._pro = pro;
        }

        /// <summary>
        /// 产品列表
        /// </summary>
        /// <returns></returns>
        public IActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 产品修改
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string id)
        {
            var result = _pro.GetSpeciesManage(id);
            var json = JsonConvert.SerializeObject(result.Result);
            JObject obj = JObject.Parse(json);
            SpeciesManageModels entity = JsonConvert.DeserializeObject<SpeciesManageModels>(obj["Data"].ToString());

            return View(entity);
        }

        /// <summary>
        /// 产品新增
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 产品列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public JsonResult GetSpeciesManageAll(int index, int size, string sasearch)
        {
            var ret = _pro.GetSpeciesManageAll(index, size, sasearch);
            return Json(ret);
        }

        /// <summary>
        /// 产品新增修改
        /// </summary>
        /// <param name="speciesId"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddUpdateSpeciesManage([FromBody]SpeciesManageModels model)
        {
            var ret = _pro.AddUpdateSpeciesManage(model);
            return Json(ret);
        }

        /// <summary>
        /// 产品删除
        /// </summary>
        /// <param name="speciesId"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult DelectSpeciesManage(string speciesId)
        {
            var ret = _pro.DelectSpeciesManage(speciesId);
            return Json(ret);
        }

        /// <summary>
        /// 产品选择列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public JsonResult GetSpeciesManageTree(string type)
        {
            var ret = _pro.GetSpeciesManageTree(type);
            return Json(ret);
        }
    }
}